Write a program in C++ to test the Type Casting

 

Assignment 9

Write a program in C++ to test the Type Casting.

Program Code with explanation:

1.    #include <iostream>

2.    using namespace std;

3.    int main()

4.    {

5.          char c ='A';// Character Data Type initilize with 'A' ASCII Value is 65 

6.          int i = 10; // Integer Data Type initilize with 10

7.          float f = 10.10; // Float Data Type initilize with 10.10

8.          double d = 12.12; // Double Data Type initilize with 12.12

9.          cout<< "*******************************************************************";

10.       cout << "\n \t\t DEMO FOR IMPLICIT DATA TYPE CASTING"<<endl;

11.       cout<< "*******************************************************************";

12.       cout << "\n  Value of Integer i before change = " << i << endl;

13.       // Character c implicitly converted to int using ASCII Value

14.       i = i + c;

15.       cout << "  Value of Integer i After change = " << i << endl;

16.       cout << "  Value of Float f before change = " << f << endl;

17.       // Integer i implicitly converted to Float

18.       f = i + f;

19.       cout << "  Value of Float f After change = " << f << endl;

20.       cout << "  Value of Double d before change = " << d << endl;

21.       // Character c implicitly converted to double using ASCII Value

22.       d = d + c;

23.       cout << "  Value of Double d After change = " << d << endl;

24.       cout<< "*******************************************************************";

25.       cout<<endl;

26.       cout << "\t \t DEMO FOR EXPLICIT DATA TYPE CASTING" << endl;

27.       cout<< "*******************************************************************";

28.       char ch = 'a'; // ASCII value of 'a' is 97

29.       float f1 = 0;

30.       cout << "\n  Value of Float f1 before change = " << f1 << endl;

31.       f1 = (double) ch;

32.       cout << "  Value of Float f1 After change = " << f1 << endl;

33.       // Explicit conversion from double to int

34.       double d1 = 20.20;

35.       int j = 0;

36.       cout << "\n  Value of Integer j before change = " << j << endl;

37.       // Use round value 20 of Double value 20.20

38.       j = (int) d1 + 10;

39.       cout << "  Value of Integer j After change = " << j << endl;

40.       int n1 = 29, n2 = 3;

41.       cout << "\n  Result of two integer division 29 div 3" << endl;

42.       float f2 = n1 / n2;

43.      cout << " Store in float f2 WITHOUT DATA TYPE CASTING = " << f2 << endl;

45.       cout << " Store in float f2 WITH DATA TYPE CASTING = " << f2 << endl;

46.       return 0;

47.  }

Explanation of Code:

Line No. 1 and 2: include the Library file and stander input – output functions.

Line No. 3: main function header.

Line No. 4 and 47: Begin and end of main function respectively.

Line No. 12 to 23: DEMO FOR IMPLICIT DATA TYPE CASTING.

Line No. 28 and 45: DEMO FOR EXPLICIT DATA TYPE CASTING.

Line No. 46: return statement appropriate with main function return data type.

Program Code for run:

#include <iostream>

using namespace std;

int main()

{

    char c ='A';// Character Data Type initilize with 'A' ASCII Value is 65 

    int i = 10; // Integer Data Type initilize with 10

    float f = 10.10; // Float Data Type initilize with 10.10

    double d = 12.12; // Double Data Type initilize with 12.12

   

    cout<< "*******************************************************************";

    cout << "\n \t\t DEMO FOR IMPLICIT DATA TYPE CASTING"<<endl;

    cout<< "*******************************************************************";

    cout << "\n  Value of Integer i before change = " << i << endl;

    // Character c implicitly converted to int using ASCII Value

    i = i + c;

    cout << "  Value of Integer i After change = " << i << endl;

   

    cout << "  Value of Float f before change = " << f << endl;

    // Integer i implicitly converted to Float

    f = i + f;

    cout << "  Value of Float f After change = " << f << endl;

   

    cout << "  Value of Double d before change = " << d << endl;

    // Character c implicitly converted to double using ASCII Value

    d = d + c;

    cout << "  Value of Double d After change = " << d << endl;

   

    cout<< "*******************************************************************";

    cout<<endl;

    cout << "\t \t DEMO FOR EXPLICIT DATA TYPE CASTING" << endl;

    cout<< "*******************************************************************";

    char ch = 'a'; // ASCII value of 'a' is 97

    float f1 = 0;

    cout << "\n  Value of Float f1 before change = " << f1 << endl;

    f1 = (double) ch;

    cout << "  Value of Float f1 After change = " << f1 << endl;

   

    // Explicit conversion from double to int

    double d1 = 20.20;

    int j = 0;

    cout << "\n  Value of Integer j before change = " << j << endl;

    // Use round value 20 of Double value 20.20

    j = (int) d1 + 10;

    cout << "  Value of Integer j After change = " << j << endl;

   

    int n1 = 29, n2 = 3;

    cout << "\n  Result of two integer division 29 div 3" << endl;

    float f2 = n1 / n2;

    cout << " Store in float f2 WITHOUT DATA TYPE CASTING = " << f2 << endl;

    f2 = (float) n1 / n2;

    cout << " Store in float f2 WITH DATA TYPE CASTING = " << f2 << endl;

    return 0;

}

Output of Program:

        *******************************************************************
                         DEMO FOR IMPLICIT DATA TYPE CASTING
        *******************************************************************
                Value of Integer i before change = 10
                Value of Integer i After change = 75
                Value of Float f before change = 10.1
                Value of Float f After change = 85.1
                Value of Double d before change = 12.12
                Value of Double d After change = 77.12
        *******************************************************************
                         DEMO FOR EXPLICIT DATA TYPE CASTING
        *******************************************************************
                Value of Float f1 before change = 0
                Value of Float f1 After change = 97
 
                Value of Integer j before change = 0
                Value of Integer j After change = 30
 
                Result of two integer division 29 div 3
                Store in float f2 WITHOUT DATA TYPE CASTING = 9
                Store in float f2 WITH DATA TYPE CASTING = 9.66667

There are two types of Type Casting:

Basic information for that as below-

Implicit Type Casting:

·         It’s done by the compiler on its own, without any external trigger from the user.

·         It done automatic by compiler at run time.

·         Implicit type casting takes place when more than one mix data type used in an expression.

·         It gives the correct result of mathematical expression.

·         Small Data types converted automatically to larger data type. Only one condition, the variable on the left side of the expression must have a larger data type than all the variables on the right.

Small Data Types                      à                   Larger Data Type

Char         ->         int        ->         float    ->        double

Explicit Type Casting:

·         It is user-defined process. It’s required for result in particular data type

·         Write the required type in front of expression in rounded parenthesis at the right side.

·         Variable = (Data Type) expression


Comments